package com.app.config;

import com.app.mapper.ComplaintMapper;
import com.app.model.ComplaintModel;
import com.app.service.ComplaintService;
import com.app.util.CommonHelp;
import com.app.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务任务配置
 */
@Slf4j
@Configuration
@EnableScheduling
public class SchedulingConfig {

    @Resource
    ComplaintMapper complaintMapper;

    @Scheduled(cron = "0 00 00 ? * *") // 每天晚上00:00触发
    public void noGr() {
        try {
            List<ComplaintModel> complaintModels = complaintMapper.selectList(null);
            for (ComplaintModel complaintModel : complaintModels) {
                //创建时间
                long createTime = complaintModel.getCreateTime().getTime();
                //当前时间
                long nowTime = CommonHelp.getNowTimestamp().getTime();
                //相差时间
                long time = nowTime - createTime;
                long day = time / 1000 / 60 / 60 / 24;
                //1 根据天数进行判断
                if (day < 20) {
                    complaintModel.setWarningStatus(1); //如果提交时间低于20天内，设置为1 绿灯
                } else if (day <= 30) {
                    complaintModel.setWarningStatus(2); //如果提交时间在20-30天，设置为2 黄灯
                } else {
                    complaintModel.setWarningStatus(3); //如果提交时间大于30天，设置为3 红灯
                }
                String circleFile = complaintModel.getCircleFile();
                Integer circleStatus = complaintModel.getCircleStatus();
                //2 根据闭环文件和闭环状态进行判断
                if (circleFile != null && circleStatus != null) {
                    complaintModel.setWarningStatus(1); //如果闭环文件有路径 有且闭环有状态 直接将状态设置为绿灯
                }
                complaintMapper.updateById(complaintModel);
            }
        } catch (Exception e) {
            log.error("定时任务出错", e);
        }
    }

    @GetMapping("/szwq/start")
    public void start() {
        noGr();
    }
}
